/**
 * 
 */
package ${basepackage}.manager.impl;

import java.util.ArrayList;
import java.util.List;

import com.excalibur.core.util.date.DateUtils;
import com.excalibur.core.util.SpringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ${basepackage}.manager.${table.generateClassName}Manager;
import ${basepackage}.manager.exception.ServiceException;
import ${basepackage}.mapper.${databaseNameCamel}.${table.generateClassName}Mapper;
import ${basepackage}.model.criteria.${databaseNameCamel}.${table.generateClassName}Criteria;
import ${basepackage}.model.po.${databaseNameCamel}.${table.generateClassName};
import ${basepackage}.model.vo.${databaseNameCamel}.${table.unCapGenerateClassName}.Add${table.generateClassName}Form;
import ${basepackage}.model.vo.${databaseNameCamel}.${table.unCapGenerateClassName}.${table.generateClassName}Row;
import ${basepackage}.model.vo.${databaseNameCamel}.${table.unCapGenerateClassName}.Update${table.generateClassName}Form;
import ${basepackage}.model.vo.common.Page;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

/**
 * @author "Jiance Qin"
 * 
 * @date 2014年2月26日
 * 
 * @time 下午6:08:48
 * 
 * @desc
 * 
 */
@Service
public class ${table.generateClassName}ManagerImpl implements ${table.generateClassName}Manager {

	protected final static Logger logger = LoggerFactory
			.getLogger(${table.generateClassName}ManagerImpl.class);

	@Autowired
	private ${table.generateClassName}Mapper ${table.unCapGenerateClassName}Mapper;

	/**
	 * 
	 */
	public ${table.generateClassName}ManagerImpl() {

	}

	@Override
	public boolean delete(${table.primaryKey.type} ${table.primaryKey.name?uncap_first}) throws ServiceException {
		if (${table.primaryKey.name?uncap_first} == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.IdNotNull"));
		}
		// 检查是否存在
		if (${table.unCapGenerateClassName}Mapper.get(${table.primaryKey.name?uncap_first}) == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.NotExist"));
		}

		${table.unCapGenerateClassName}Mapper.remove(${table.primaryKey.name?uncap_first});
		return true;
	}

	@Override
	public Page<${table.generateClassName}Row> get${table.generateClassName}s(
			${table.generateClassName}Criteria criteria, int offset, int limit)
			throws ServiceException {
		if (criteria == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.CriteriaNotNull"));
		}
		if (offset < 0) {
			offset = 0;
		}
		if (limit < 1) {
			limit = 1;
		}

		Page<${table.generateClassName}Row> ${table.unCapGenerateClassName}List = new Page<${table.generateClassName}Row>();
		PageList<${table.generateClassName}> list = ${table.unCapGenerateClassName}Mapper.getList(criteria, new PageBounds(offset,
				limit, true));
		${table.unCapGenerateClassName}List.setTotal(Long.valueOf(list.getPaginator().getTotalCount()));

		List<${table.generateClassName}Row> rowList = create${table.generateClassName}RowList(list);
		${table.unCapGenerateClassName}List.setRows(rowList);

		return ${table.unCapGenerateClassName}List;
	}

	/**
	 * 创建前台显示列表.
	 * 
	 * @param list
	 * @return
	 * @throws ServiceException
	 */
	protected List<${table.generateClassName}Row> create${table.generateClassName}RowList(
			List<${table.generateClassName}> list) throws ServiceException {
		if (list == null || list.isEmpty()) {
			return null;
		}
		List<${table.generateClassName}Row> rowList = new ArrayList<${table.generateClassName}Row>();
		${table.generateClassName}Row row;
		for (${table.generateClassName} ${table.unCapGenerateClassName} : list) {
			row = create${table.generateClassName}Row(${table.unCapGenerateClassName});
			if (row != null) {
				rowList.add(row);
			}
		}
		return rowList;
	}

	/**
	 * 创建前台显示对象.
	 * 
	 * @param ${table.unCapGenerateClassName}
	 * @return
	 * @throws ServiceException
	 */
	protected ${table.generateClassName}Row create${table.generateClassName}Row(${table.generateClassName} ${table.unCapGenerateClassName})
			throws ServiceException {
		if (${table.unCapGenerateClassName} == null) {
			return null;
		}
		${table.generateClassName}Row row = new ${table.generateClassName}Row();
		
		<#list table.columns as column>
	    <#if column.type == "Date">
		row.set${column.name}(DateUtils.formatAsString(${table.unCapGenerateClassName}.get${column.name}()));
	    <#else>
		row.set${column.name}(${table.unCapGenerateClassName}.get${column.name}());
	    </#if>
		</#list>
		
		return row;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.general.op.manager.${table.generateClassName}Manager#add${table.generateClassName}(com.general
	 * .op.model.vo .Add${table.generateClassName}Form)
	 */
	@Override
	public ${table.generateClassName}Row add${table.generateClassName}(
			Add${table.generateClassName}Form add${table.generateClassName}Form) throws ServiceException {
		if (add${table.generateClassName}Form == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AddFormNotNull"));
		}

		${table.generateClassName} ${table.unCapGenerateClassName} = createNew${table.generateClassName}(add${table.generateClassName}Form);
		${table.primaryKey.type} add${table.primaryKey.name} = ${table.unCapGenerateClassName}Mapper.insert(${table.unCapGenerateClassName});
		${table.unCapGenerateClassName} = ${table.unCapGenerateClassName}Mapper.get(add${table.primaryKey.name});
		return create${table.generateClassName}Row(${table.unCapGenerateClassName});
	}

	/**
	 * 创建新外部Url.
	 * 
	 * @param add${table.generateClassName}Form
	 * @return
	 */
	protected ${table.generateClassName} createNew${table.generateClassName}(
			Add${table.generateClassName}Form add${table.generateClassName}Form) {
		${table.generateClassName} new${table.generateClassName} = new ${table.generateClassName}();

		<#list table.commonColumns as column>
	    <#if column.type != "Date">
		new${table.generateClassName}.set${column.name}(add${table.generateClassName}Form.get${column.name}());
	    </#if>
		</#list>

		return new${table.generateClassName};
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.general.op.manager.${table.generateClassName}Manager#update${table.generateClassName}(com
	 * .general.op.model .vo.Update${table.generateClassName}Form)
	 */
	@Override
	public ${table.generateClassName}Row update${table.generateClassName}(
			Update${table.generateClassName}Form update${table.generateClassName}Form)
			throws ServiceException {
		if (update${table.generateClassName}Form == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.UpdateFormNotNull"));
		}
		if (update${table.generateClassName}Form.get${table.primaryKey.name}() == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.IdNotNull"));
		}
		// 检查是否存在
		${table.generateClassName} ${table.unCapGenerateClassName} = ${table.unCapGenerateClassName}Mapper.get(update${table.generateClassName}Form
				.get${table.primaryKey.name}());
		if (${table.unCapGenerateClassName} == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.NotExist"));
		}
		${table.unCapGenerateClassName} = createUpdate${table.generateClassName}(update${table.generateClassName}Form);
		Integer ret = ${table.unCapGenerateClassName}Mapper.update(${table.unCapGenerateClassName});
		${table.unCapGenerateClassName} = ${table.unCapGenerateClassName}Mapper.get(${table.unCapGenerateClassName}.get${table.primaryKey.name}());
		return create${table.generateClassName}Row(${table.unCapGenerateClassName});
	}

	/**
	 * 修改.
	 * 
	 * @param update${table.generateClassName}Form
	 * @return
	 */
	protected ${table.generateClassName} createUpdate${table.generateClassName}(
			Update${table.generateClassName}Form update${table.generateClassName}Form) {
		${table.generateClassName} update${table.generateClassName} = new ${table.generateClassName}();

		<#list table.columns as column>
	    <#if column.type != "Date">
	    update${table.generateClassName}.set${column.name}(update${table.generateClassName}Form.get${column.name}());
	    </#if>
		</#list>

		return update${table.generateClassName};
	}
}
